package com.qf.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCouponMapper;
import com.qf.mapper.DtsCouponUserMapper;
import com.qf.pojo.DtsCoupon;
import com.qf.pojo.DtsCouponUser;
import com.qf.service.AdminCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;


@Service
public class AdminCouponServiceImpl implements AdminCouponService {

    @Autowired
    private DtsCouponMapper dtsCouponMapper;

    @Autowired
    private DtsCouponUserMapper dtsCouponUserMapper;

    @Override
    public IPage<DtsCoupon> selectCouponList(Integer page, Integer limit, String name, Integer type, Integer status, String sort, String order) {

        IPage<DtsCoupon> iPage = new Page<DtsCoupon>(page, limit);

        QueryWrapper<DtsCoupon> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted", false);
        wrapper.orderByAsc("asc".equalsIgnoreCase(order),sort);
        wrapper.orderByDesc("desc".equalsIgnoreCase(order),sort);
        wrapper.like(!StringUtils.isEmpty(name), "`name`", name);
        wrapper.eq(type != null, "type", type);
        wrapper.eq(status!= null, "status", status);

        IPage<DtsCoupon> result = dtsCouponMapper.selectPage(iPage, wrapper);

        return result;
    }

    @Override
    public DtsCoupon selectCouponById(Integer id) {

        DtsCoupon dtsCoupon = dtsCouponMapper.selectById(id);

        return dtsCoupon;
    }

    @Override
    public IPage<DtsCouponUser> selectCouponUserList(Integer page, Integer limit, Integer couponId, Integer userId, Integer status, String sort, String order) {

        IPage<DtsCouponUser> iPage = new Page<>(page, limit);

        QueryWrapper<DtsCouponUser> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted" , false);
        wrapper.eq("coupon_id" , couponId);
        wrapper.orderByAsc("asc".equalsIgnoreCase(order),sort);
        wrapper.orderByDesc("desc".equalsIgnoreCase(order),sort);
        wrapper.eq(userId!= null, "user_id", userId);
        wrapper.eq(status!= null, "status", status);

        IPage<DtsCouponUser> result = dtsCouponUserMapper.selectPage(iPage, wrapper);

        return result;
    }

    @Override
    public DtsCoupon insertCoupon(DtsCoupon dtsCoupon) {

        dtsCoupon.setAddTime(new Date());
        dtsCoupon.setDeleted(false);
        dtsCoupon.setUpdateTime(new Date());

        dtsCouponMapper.insert(dtsCoupon);

        QueryWrapper<DtsCoupon> wrapper = new QueryWrapper<>();
        wrapper.select("id");
        wrapper.eq("`name`" , dtsCoupon.getName());

        DtsCoupon dtsCoupon1 = dtsCouponMapper.selectOne(wrapper);

        dtsCoupon.setId(dtsCoupon1.getId());

        return dtsCoupon;
    }

    @Override
    public DtsCoupon updateCoupon(DtsCoupon dtsCoupon) {
        dtsCoupon.setUpdateTime(new Date());
        dtsCouponMapper.updateById(dtsCoupon);
        return dtsCoupon;
    }

    @Override
    public void deleteCoupon(Integer id) {
        DtsCoupon dtsCoupon = new DtsCoupon();
        dtsCoupon.setId(id);
        dtsCoupon.setDeleted(true);
        dtsCouponMapper.updateById(dtsCoupon);
    }
}
